//
//  HMDownLoadViewController.m
//  支付宝项目
//
//  Created by mac on 17/1/3.
//  Copyright © 2017年 mac.gu. All rights reserved.
//


//测试灯泡
#import "HMBaseViewController.h"

//已购买
#import "HMBuyViewController.h"
//新闻
#import "HMNewsViewController.h"
//订阅
#import "HMSubscribeViewController.h"
#import "Masonry.h"
#import "HMDownLoadViewController.h"

@interface HMDownLoadViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *selectedButton;

//@property (nonatomic, strong) UIView *lineView;

//@property (nonatomic, strong) UIButton *firstButton;

@end

@implementation HMDownLoadViewController{
    UIView *_categoryView;
    UIScrollView * _contentView;
    UISegmentedControl *_segmentControl;
    UIView *_downLoadView;
    UIButton *_allDownLoadBtn;
    UIButton *_allDeleteBtn;
    //中间的线
    UIView *_lineDownView;
    
    //下面line
    UIView *_lineView;
    UIButton *_firstButton;
    
    
    UIView *_middleTwoLineView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatSegmentControl];
    
    if (_segmentControl.selectedSegmentIndex == 0) {
        [self setupDownLoadUI];
        
        //内容详情
        [self createContentView];
    }
    
}
//添加UISegmentedControl

- (void)creatSegmentControl{
    
    NSArray *segmentArr =[NSArray arrayWithObjects:@"已下载",@"下载中", nil];
    
    _segmentControl = [[UISegmentedControl alloc]initWithItems:segmentArr];
    
    _segmentControl.frame = CGRectMake(0, 0, 200 , 30);
    _segmentControl.selectedSegmentIndex = 0;
    [self.navigationItem setTitleView:_segmentControl];
    
    
    [_segmentControl addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
}

//点击segement切换视图
- (void)segmentClick:(UISegmentedControl *)segentControl{
    
    //创建
    NSInteger index = segentControl.selectedSegmentIndex;
    
    switch (index) {
        case 0:
            
            [_downLoadView removeFromSuperview];
            
            [self setupDownLoadUI];
            
            [self createContentView];
            break;
        case 1:
            [_categoryView removeFromSuperview];
            [_contentView removeFromSuperview];
            
            [self setupDownloadingUI];
            break;
        default:
            break;
    }
    
}


//已经下载
- (void)setupDownLoadUI{
    
    //设置view的fream;
    _categoryView = [[UIView alloc]init];
    
    [self.view addSubview:_categoryView];
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.offset(50);
    }];
    
    
    NSArray *titleArray = @[@"知识新闻",@"已下载",@"订阅"];
    //创建分类按钮
    NSMutableArray *buttonArrM = [NSMutableArray arrayWithCapacity:3];
    for (NSInteger i = 0; i < titleArray.count; i++) {
        
        UIButton *titleButton = [[UIButton alloc]init];
        [titleButton setTitle:titleArray[i] forState:UIControlStateNormal];
        
        //设置按钮颜色  导入颜色分类 0x656565 0x2f2f2f
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        //给button设置tag
        titleButton.tag = i;
        
        if (i == 0)
        {
            titleButton.selected = YES;
            self.selectedButton = titleButton;
            
        }
        
        
        [titleButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
#warning --------bug
        //        [self.view addSubview:titleButton];
        [_categoryView addSubview:titleButton];
        //把按钮添加到可变数组
        [buttonArrM addObject:titleButton];
        
    }
    //设置按钮的约束
    [buttonArrM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    
    [buttonArrM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(_categoryView);
    }];
    
    //获取第一个按钮
    _firstButton = buttonArrM[0];
    
    UIButton *secondButton = buttonArrM[1];
    
    //设置按钮下面的线
    _lineView = [[UIView alloc]init];
    
    _lineView.backgroundColor = [UIColor orangeColor];
#warning --------线
    [_categoryView addSubview:_lineView];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.centerX.equalTo(_firstButton);
        make.width.offset(50);
        make.height.offset(4);
    }];
    
    
    
    
    //设置view最下面的线
#warning --------
    UIView *longLineView = [[UIView alloc]init];
    [_categoryView addSubview:longLineView];
    longLineView.backgroundColor = [UIColor grayColor];
    [longLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(_categoryView);
        make.width.equalTo(self.view);
        make.height.offset(1);
    }];
    
    //设置view中间线
#warning -------
    UIView *middleLineView = [[UIView alloc]init];
    [_categoryView addSubview:middleLineView];
    middleLineView.backgroundColor = [UIColor grayColor];
    [middleLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_firstButton).offset(0.5);
        make.centerY.equalTo(_firstButton);
        make.width.offset(1);
        make.height.offset(26);
    }];
#warning ----------
    _middleTwoLineView = [[UIView alloc]init];
    [_categoryView addSubview:_middleTwoLineView];
    _middleTwoLineView.backgroundColor = [UIColor grayColor];
    [_middleTwoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(secondButton).offset(0.5);
        make.centerY.equalTo(secondButton);
        make.width.offset(1);
        make.height.offset(26);
    }];
    
    
}

- (void) createContentView
{
    //创建scollerView
    _contentView = [[UIScrollView alloc] init];
    
    _contentView.backgroundColor = [UIColor purpleColor];
    
    //设置代理
    _contentView.delegate = self;
    
    //设置分页效果
    _contentView.pagingEnabled = YES;
    
    //取消滚动条
    _contentView.showsVerticalScrollIndicator = NO;
    _contentView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:_contentView];
    
    _contentView.scrollEnabled = NO;
    
    //    self.contentView = contentView;
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(_categoryView.mas_bottom);
    }];
    
    //创建三个控制器
    HMNewsViewController * newsVC = [[HMNewsViewController alloc] init];
    HMBuyViewController * buyVC = [[HMBuyViewController alloc] init];
    HMSubscribeViewController* subscribeVC = [[HMSubscribeViewController alloc] init];
    
    [_contentView addSubview:newsVC.view];
    [_contentView addSubview:buyVC.view];
    [_contentView addSubview:subscribeVC.view];
    
    //建立父子关系
    [self addChildViewController:newsVC];
    [self addChildViewController:buyVC];
    [self addChildViewController:subscribeVC];
    
    //告诉系统 已经建立父子关系
    [newsVC didMoveToParentViewController:self];
    [buyVC didMoveToParentViewController:self];
    [subscribeVC didMoveToParentViewController:self];
    
    
    //    布局
    [_contentView.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    //确定上下和大小
    [_contentView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(_contentView);
        make.size.equalTo(_contentView);
    }];
    
    
}



//分类按钮的点击事件
- (void) buttonClick:(UIButton *)sender{
    //
    self.selectedButton.selected = NO;
    self.selectedButton.userInteractionEnabled = YES;
    //点击的按钮选中状态时YES
    sender.selected = YES;
    
    sender.userInteractionEnabled = NO;
    
    self.selectedButton = sender;
    //
    [UIView animateWithDuration:0.3 animations:^
     {
         
         
         [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
             
             make.centerX.equalTo(_firstButton).offset(sender.tag * _firstButton.bounds.size.width);
             
         }];
         [self.view layoutIfNeeded];
     }];
    
    
    //设置 内容视图 (scollerView) 的  偏移
    [_contentView setContentOffset:CGPointMake(sender.tag * _contentView.bounds.size.width, 0) animated:YES];
    
    
}



//下载中
- (void)setupDownloadingUI{
    
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    _downLoadView = [[UIView alloc]init];
    
    _downLoadView.backgroundColor = [UIColor greenColor];
    
    [self.view addSubview:_downLoadView];
    [_downLoadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.offset(50);
    }];
    
    //创建全部下载按钮
    _allDownLoadBtn = [[UIButton alloc]init];
    [_allDownLoadBtn setTitle:@"全部下载" forState:UIControlStateNormal];
    [_allDownLoadBtn setImage:[UIImage imageNamed:@"new_today_audio_download_icon_30x30_"] forState:UIControlStateNormal];
    
    [_downLoadView addSubview:_allDownLoadBtn];
    [_allDownLoadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_downLoadView);
        make.height.equalTo(_downLoadView);
        make.width.equalTo(_downLoadView.mas_width).multipliedBy(0.5);
    }];
    
    //全部删除按钮
    _allDeleteBtn = [[UIButton alloc]init];
    [_allDeleteBtn setTitle:@"全部删除" forState:UIControlStateNormal];
    [_allDeleteBtn setImage:[UIImage imageNamed:@"download_clear_image_44x44_"] forState:UIControlStateNormal];
    
    [_downLoadView addSubview:_allDeleteBtn];
    [_allDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_downLoadView);
        make.height.equalTo(_allDownLoadBtn);
        make.width.equalTo(_allDownLoadBtn);
    }];
    
    [_allDownLoadBtn addTarget:self action:@selector(allDownLoadBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_allDeleteBtn addTarget:self action:@selector(downDeleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    //创建两个按钮中间的线
    _lineDownView = [[UIView alloc]init];
    _lineDownView.backgroundColor = [UIColor grayColor];
    [_downLoadView addSubview:_lineDownView];
    [_lineDownView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.centerY.equalTo(_downLoadView);
        make.height.offset(40);
        make.width.offset(1);
    }];
}
//全部下载的按钮点击事件
- (void)allDownLoadBtnClick{
    
}
//全部清空的点击事件
- (void)downDeleteBtnClick{
    HMBaseViewController *base =[[HMBaseViewController alloc]init];
    [self.navigationController pushViewController:base animated:YES];
}

@end



